package com.harmontronics.hanma.paas.paas.controller;

import com.harmontronics.hanma.paas.paas.domain.iam.entity.PermActionList;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import com.harmontronics.hanma.paas.paas.common.PageBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.harmontronics.hanma.paas.paas.service.HmPermAppService;
import com.harmontronics.hanma.paas.paas.dto.iam.output.HmPermResult;
import com.harmontronics.hanma.paas.paas.dto.iam.input.HmPermCreateInput;
import com.harmontronics.hanma.paas.paas.dto.iam.input.HmPermUpdateInput;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.HmPerm;
import com.harmontronics.hanma.paas.paas.utils.ParamUtils;

import javax.validation.Valid;


/**
 * 权限表
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-04-07 10:35:44
 */
@RestController
@RequestMapping("/api/hmperm")
@Api(tags = {"权限表"})
public class HmPermController {
    @Autowired
    private HmPermAppService hmPermappService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("获取所有菜单")
    public ResponseEntity<List<PermActionList>> list(@RequestParam Long appId, @RequestParam(required = false) Integer status) {
        List<PermActionList> hmPermList = hmPermappService.selectAllPermAndAction(appId,status);
        return ResponseEntity.ok(hmPermList);
    }

    /**
    * 分页列表
    */
    @GetMapping("/page/list")
    @ApiOperation("分页列表")
    public ResponseEntity<PageBase<HmPermResult>> list(@ApiParam(value = "页码", defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNo,
            @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize) {
        PageBase<HmPerm> hmPermPageBase = hmPermappService.getList(pageNo, pageSize);
        PageBase<HmPermResult> resultPageBase = PageBase.copy(hmPermPageBase,HmPermResult.class);
        return ResponseEntity.ok(resultPageBase);
    }


    @GetMapping("/action/list")
    @ApiOperation(value = "获取当前用户所有菜单")
    public ResponseEntity<List<PermActionList>> getPermActionList(@RequestParam Long appId, @RequestParam Long userId) {
        return ResponseEntity.ok(hmPermappService.getPermActionList(userId,appId));
    }


    /**
     * 信息
     */
    @GetMapping("/info")
    @ApiOperation("详情")
    public ResponseEntity<HmPermResult> info(@RequestParam Long id) {
        HmPerm hmPerm =hmPermappService.findById(id);
            HmPermResult result = BeanUtil.toBean(hmPerm,HmPermResult.class);
        return ResponseEntity.ok(result);
    }

    /**
     * 保存
     */
    @PostMapping()
    @ApiOperation("保存")
    public ResponseEntity<String> save(@Valid @RequestBody HmPermCreateInput hmPermInput,BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmPerm hmPerm =BeanUtil.toBean(hmPermInput,HmPerm.class);
        hmPermappService.save(hmPerm);

        return ResponseEntity.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    public ResponseEntity<String> update(@Valid @RequestBody HmPermUpdateInput hmPermInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmPerm hmPerm =BeanUtil.toBean(hmPermInput,HmPerm.class);
            hmPermappService.update(hmPerm);
        return ResponseEntity.ok();
    }

    @PutMapping("updatePermAndAction")
    @ApiOperation(value = "修改菜单动作")
    public ResponseEntity updatePermAndAction(@RequestBody HmPermUpdateInput updateDto) {
        hmPermappService.updatePermAndAction(updateDto.getId(),updateDto.getActions());
        return ResponseEntity.ok();
    }

    @PutMapping("updatePermStatus")
    @ApiOperation(value = "启用/禁用权限")
    public ResponseEntity update(@RequestBody HmPermUpdateInput statusDto) {
        HmPerm hmPerm = BeanUtil.toBean(statusDto,HmPerm.class);
        hmPermappService.updatePerm(hmPerm);
        return ResponseEntity.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping()
    @ApiOperation("删除")
    public ResponseEntity<String> delete(@RequestBody Long[] ids) {
        hmPermappService.remove(Arrays.asList(ids));
        return ResponseEntity.ok();
    }

    @GetMapping("/validate-field")
    @ApiOperation(value = "表单字段验证")
    public ResponseEntity validateField(
            @RequestParam long id,
            @RequestParam Long appId,
            @RequestParam String key,
            @RequestParam String value,
            @RequestParam long parentId) {
        hmPermappService.validateField(id, appId, key, value, parentId);
        return ResponseEntity.ok();
    }

}
